# !/usr/bin/python
# -*- coding: utf-8 -*-
# @Project    : AutomatedTesting
# @Name       : get_time.py
# @IDE        : PyCharm
# @Author     : OneYoung
# @Created    : 2025/8/25 14:26
# @Desc       : 封装常用的时间获取方法,方便使用

import time
import datetime


def str_date() -> str:
    """ 当前日期(%Y-%m-%d) """
    return time.strftime("%Y-%m-%d")


def str_time() -> str:
    """ 当前时间(%H:%M:%S) """
    return time.strftime("%H:%M:%S")


def str_datetime() -> str:
    """ 当前日期时间(%Y-%m-%d %H:%M:%S) """
    return time.strftime("%Y-%m-%d %H:%M:%S")


def digital_datetime() -> str:
    """ 当前日期时间(%Y%m%d%H%M%S) """
    return time.strftime("%Y%m%d%H%M%S")


def get_str_datetime(f) -> str:
    """
    获取特定的字符串日期时间

    :param f: 字符串格式，如：%Y-%m-%d
    """
    return time.strftime(f)


def datetime_to_str(date_time) -> str:
    """
    日期类型转字符串日期时间(%Y-%m-%d %H:%M:%S)

    :param date_time: 需要转换的日期数据
    """
    return date_time.strftime("%Y-%m-%d %H:%M:%S")


def date_today():
    """返回当前日期"""
    return datetime.date.today()


def datetime_now():
    """返回当前时间"""
    return datetime.datetime.now()


def timestamp_to_str(timestamp):
    """
    时间戳转字符串日期时间

    :param timestamp: 时间戳
    """
    if len(str(timestamp)) > 10:
        timestamp = int(str(timestamp)[:10])
    return datetime.datetime.fromtimestamp(timestamp)


def str_to_timestamp(string, millisecond=False):
    """字符串日期时间转时间戳

    :param string: 格式: %Y-%m-%d %H:%M:%S
    :param millisecond: 是否返回13位时间戳,默认10位
    """
    time_tuple = time.strptime(string, "%Y-%m-%d %H:%M:%S")
    stamp = time.mktime(time_tuple)
    timestamp = int(stamp)
    if millisecond:
        timestamp = int(stamp * 1000)
    return timestamp


def timestamp_now(millisecond=False):
    """
    返回当前时间戳

    :param millisecond: 是否返回13位时间戳,默认10位
    """
    dt = datetime.datetime.now()
    stamp = datetime.datetime.timestamp(dt)
    timestamp = int(stamp)
    if millisecond:
        timestamp = int(stamp * 1000)
    return timestamp


def offset_str_datetime(**delta):
    """获取带偏移量的日期时间

    :param delta: weeks/days/hours/minutes/seconds/milliseconds/microseconds
    """
    offset = datetime.timedelta(**delta)
    except_time = datetime_now() + offset
    return datetime_to_str(except_time)


def offset_timestamp(millisecond=False, **delta):
    """获取带偏移量的时间戳

    :param millisecond: 返回13位时间戳,默认10位
    :param delta: weeks/days/hours/minutes/seconds/milliseconds/microseconds
    """
    offset = offset_str_datetime(**delta)
    return str_to_timestamp(string=offset, millisecond=millisecond)
